package com.sch.wanandroid.ui.main.tab

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.adapter.TabFragmentAdapter
import com.sch.wanandroid.base.BaseFragment
import com.sch.wanandroid.entity.TabTypeBean
import com.sch.wanandroid.ui.main.tab.list.TabListFragment
import kotlinx.android.synthetic.main.fragment_tab.*

/**
 * 项目页和公众号 共用
 */
class TabFragment : BaseFragment() {
    companion object {
        fun newInstance(type: Int) =
            TabFragment().apply {
                arguments = Bundle().apply {
                    putInt("type", type)
                }
            }
    }

    private lateinit var tabViewModel: TabViewModel

    private val fragmentAdapter by lazy {
        TabFragmentAdapter(
            childFragmentManager
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        tabViewModel = ViewModelProvider(this).get(TabViewModel::class.java)
        return inflater.inflate(R.layout.fragment_tab, null)
    }

    var type: Int = 0
    override fun init(savedInstanceState: Bundle?) {
        //获取type，用于判断当前是项目页还是公众号页面
        type = arguments?.getInt("type")!!
        viewPager.adapter = fragmentAdapter
        viewPager.offscreenPageLimit = 5
        //tabLayout 和viewPager绑定
        tabLayout.setupWithViewPager(viewPager)
        initListener()
        //获取tab分类数据
        loadingTip.loading()
        tabViewModel.getTabListData(type)
        tabViewModel.tabListLiveData.observe(this, Observer {
            loadingTip.dismiss()
            initListFragment(it)
        })
        tabViewModel.errorMsg.observe(this, Observer {
            loadingTip.showInternetError()
            SmartToast.fail(it)
        })

    }

    private fun initListener() {
        // 设置无网络时重新加载点击事件
        loadingTip.setReloadListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                loadingTip.loading()
                tabViewModel.getTabListData(type)
            }
        })
    }

    private fun initListFragment(tabList: List<TabTypeBean>) {
        val fragmentList = ArrayList<Fragment>()
        tabList.forEach {
            val tabListFragment = TabListFragment()
            val bundle = Bundle()
            bundle.putInt("id", it.id)
            type?.let { it1 -> bundle.putInt("type", it1) }
            bundle.putString("name", it.name)
            tabListFragment.arguments = bundle
            fragmentList.add(tabListFragment)
        }
        fragmentAdapter.updata(fragmentList, tabList)
    }

    fun scrollToTop() {
        if (fragmentAdapter.count > 0) {
            val fragment: TabListFragment =
                fragmentAdapter.getItem(viewPager.currentItem) as TabListFragment
            fragment.scrollToTop()
        }
    }
}